package cn.itcast.generate.log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class GenerateLog {
    public static String[] url_paths = {
            "GET article/112.html HTTP/1.1",
            "GET article/113.html HTTP/1.1",
            "GET article/114.html HTTP/1.1",
            "GET article/115.html HTTP/1.1",
            "GET article/116.html HTTP/1.1",
            "GET article/117.html HTTP/1.1",
            "GET article/118.html HTTP/1.1",
            "GET article/119.html HTTP/1.1",
            "POST video/821 HTTP/1.1",
            "GET tag/list HTTP/1.1"
    };//模拟不同的http请求有get请求有post请求，放到url_path数组中
    public static String[] ip_splices =
            {"102", "71", "145", "33", "67", "54", "164", "121"};//模拟ip地址额的各个部分用于随机组成一个ip地址
    public static String[] status_codes = {"200", "404", "500"};//设置访问的状态码都有什么200成功 404为错误，500为服务器错误

    public static void main(String[] args)
            throws InterruptedException, IOException, ParseException {

    }

    public static String generateLog() throws ParseException {//生成一行日志信息，包含随机的 IP 地址、时间、URL 和状态码。
        String logInfo = randomIp()+"\t" + getDate()+ "\t" + "\""+ randomUrl()+ "\"" + "\t" +randomCode();
        return logInfo;
    }

    //随机生成IP地址
    public static String randomIp() {//生成随机的 IP 地址。通过从 ip_splices 数组中随机选择四个数值，组成一个 IP 地址。
        int ipNum;
        String ip = "";
        for (int i = 0; i < 4; i++) {
            ipNum = new Random().nextInt(8);
            ip += "." + ip_splices[ipNum];
        }
        return ip.substring(1);
    }

    //随机生成状态码
    public static String randomCode() {//从 status_codes 数组中随机选择一个 HTTP 状态码。
        int codeNum = new Random().nextInt(3);
        return status_codes[codeNum];
    }

    //随机生成URL
    public static String randomUrl() {//从 url_paths 数组中随机选择一个 URL。
        int urlNum = new Random().nextInt(10);
        return url_paths[urlNum];
    }

    //随机生成时间
    public static String getDate() throws ParseException {//生成一个随机时间戳，格式为 [dd/mmm/yyyy:hh:mm:ss +0800]
        Random random = new Random();
        String date = new String();
        DateFormat dateFormat =
                new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);
        String days = String.valueOf(random.nextInt(30) + 1);
        String hour = String.valueOf(random.nextInt(24));
        String minute = String.valueOf(random.nextInt(60));
        String second = String.valueOf(random.nextInt(60));
        date = days + "/"
                + "Aug" + "/"
                + "2022" + ":"
                + hour + ":"
                + minute + ":"
                + second;
        Date parseDate = dateFormat.parse(date);
        String formatDate = dateFormat.format(parseDate);
        return "["+formatDate+"\t"+"+0800]";
    }
}